package com.ljx.itbbs.server.article.controller;

import cn.hutool.http.server.HttpServerResponse;
import com.ljx.itbbs.common.domain.model.LoginUser;
import com.ljx.itbbs.common.domain.vo.resp.ApiResult;
import com.ljx.itbbs.common.domain.vo.resp.CursorResp;
import com.ljx.itbbs.common.domain.vo.resp.PageResp;
import com.ljx.itbbs.common.utils.SecurityUtil;
import com.ljx.itbbs.common.utils.ServletUtil;
import com.ljx.itbbs.server.article.domain.vo.req.*;
import com.ljx.itbbs.server.article.domain.vo.resp.ArticleCategoryResp;
import com.ljx.itbbs.server.article.domain.vo.resp.ArticleInfoResp;
import com.ljx.itbbs.server.article.domain.vo.resp.ArticleUpdateInfoResp;
import com.ljx.itbbs.server.article.domain.vo.resp.CommentResp;
import com.ljx.itbbs.server.article.service.ArticleCategoryService;
import com.ljx.itbbs.server.article.service.ArticleService;
import com.ljx.itbbs.server.article.service.CommentService;
import com.ljx.itbbs.server.article.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "文章接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ResourceService resourceService;

    @ApiOperation("获取所有文章类型")
    @GetMapping("/category/all")
    public ApiResult<List<ArticleCategoryResp>> getAllCategory(){

        return ApiResult.success(articleCategoryService.getAllCategory());
    }

    @ApiOperation("发布文章")
    @PostMapping("/post")
    public ApiResult<Long> postArticle(@Valid @RequestBody ArticleSaveReq req){

        return ApiResult.success(articleService.postArticle(SecurityUtil.getLoginUser().getUserId(), req));
    }

    @ApiOperation("获取文章列表")
    @GetMapping("/list")
    public ApiResult<PageResp<ArticleInfoResp>> getArticleList(@Valid ArticleListReq articleListReq){
        LoginUser loginUser = SecurityUtil.getLoginUser();

        return ApiResult.success(articleService.getArticleList(loginUser, articleListReq));
    }

    @ApiOperation("获取文章详情")
    @GetMapping("/info")
    public ApiResult<ArticleInfoResp> getArticleInfo(@Valid ArticleInfoReq req){

        return ApiResult.success(articleService.getArticleInfo(req));
    }

    @ApiOperation("修改文章信息")
    @PutMapping("/update/info")
    public ApiResult<Void> updateInfo(@Valid @RequestBody ArticleUpdateReq req){
        articleService.updateInfo(SecurityUtil.getLoginUser().getUserId(), req);

        return ApiResult.success();
    }

    @ApiOperation("获取最新的文章")
    @GetMapping("/new")
    public ApiResult<List<ArticleInfoResp>> getNewArticleInfo(@Valid ArticleNewReq req){

        return ApiResult.success(articleService.getNewArticleInfo(req));
    }

    @ApiOperation("获取热门的文章")
    @GetMapping("/hot")
    public ApiResult<List<ArticleInfoResp>> getHotArticleInfo(){

        return ApiResult.success(articleService.getHotArticleInfo());
    }

    @ApiOperation("查询文章评论")
    @GetMapping("/comment")
    public ApiResult<CursorResp<CommentResp>> getArticleComment(@Valid ArticleCommentReq req){

        return ApiResult.success(commentService.getArticleComment(req));
    }

    @ApiOperation("点赞文章")
    @PostMapping("/like/{articleId}")
    public ApiResult<Void> likeArticle(@PathVariable Long articleId){
        articleService.likeArticle(SecurityUtil.getLoginUser().getUserId(), articleId);

        return ApiResult.success();
    }

    @ApiOperation("收藏文章")
    @PostMapping("/collect/{articleId}")
    public ApiResult<Void> collectArticle(@PathVariable Long articleId){
        articleService.collectArticle(SecurityUtil.getLoginUser().getUserId(), articleId);

        return ApiResult.success();
    }

    @ApiOperation("修改文章状态")
    @PutMapping("/update/status/{articleId}/{status}")
    public ApiResult<Void> updateArticleStatus(@PathVariable Long articleId,
                                          @PathVariable Integer status){
        articleService.updateStatus(SecurityUtil.getLoginUser().getUserId(), articleId, status);

        return ApiResult.success();
    }

    @ApiOperation("删除文章")
    @DeleteMapping("/delete/{articleId}")
    public ApiResult<Void> deleteArticle(@PathVariable Long articleId){
        articleService.deleteArticle(SecurityUtil.getLoginUser().getUserId(), articleId);

        return ApiResult.success();
    }

    @ApiOperation("获取收藏文章列表")
    @GetMapping("/collect/list")
    public ApiResult<PageResp<ArticleInfoResp>> getCollectArticleList(@Valid ArticleCollectReq req){

        return ApiResult.success(articleService.getArticleCollectList(SecurityUtil.getLoginUser().getUserId(), req));
    }

    @ApiOperation("删除收藏文章")
    @PutMapping("/collect/delete/{articleId}")
    public ApiResult<Void> deleteCollectArticle(@PathVariable("articleId") Long articleId){
        articleService.deleteCollectArticle(SecurityUtil.getLoginUser().getUserId(), articleId);

        return ApiResult.success();
    }


    @ApiOperation("初始化保存文章文件资源")
    @PostMapping("/resource/upload")
    public ApiResult<Long> uploadArticleResource(@Valid @RequestBody ArticleResourceUploadReq req){

        return ApiResult.success(resourceService.uploadArticleResource(SecurityUtil.getLoginUser().getUserId(), req));
    }

    @ApiOperation("下载文章资源")
    @GetMapping("/resource/download/{resourceId}")
    public void downloadArticleResource(@PathVariable("resourceId") Long resourceId,
                                        HttpServletResponse response){

        resourceService.download(ServletUtil.getUserId(), resourceId, response);
    }


    @ApiOperation("获取修改文章详情")
    @GetMapping("/update/info")
    public ApiResult<ArticleUpdateInfoResp> getArticleUpdateInfo(@Valid ArticleInfoReq req){

        return ApiResult.success(articleService.getArticleUpdateInfo(ServletUtil.getUserId(), req));
    }

    @ApiOperation("保存文章草稿")
    @PostMapping("/draft/save")
    public ApiResult<Void> saveArticleDraft(@Valid @RequestBody ArticleSaveReq req){
        articleService.saveArticleDraft(SecurityUtil.getLoginUser().getUserId(), req);

        return ApiResult.success();
    }

    @ApiOperation("修改文章草稿信息")
    @PutMapping("/draft/update/info")
    public ApiResult<Void> updateDraftInfo(@Valid @RequestBody ArticleUpdateReq req){
        articleService.updateDraftInfo(SecurityUtil.getLoginUser().getUserId(), req);

        return ApiResult.success();
    }

    @ApiOperation("删除草稿文件")
    @DeleteMapping("/draft/delete/{articleId}")
    public ApiResult<Void> deleteDraftArticle(@PathVariable Long articleId){
        articleService.deleteDraftArticle(SecurityUtil.getLoginUser().getUserId(), articleId);

        return ApiResult.success();
    }

    @ApiOperation("获取文章草稿")
    @GetMapping("/draft/info")
    public ApiResult<ArticleUpdateInfoResp> getArticleDraftInfo(@Valid ArticleInfoReq req){

        return ApiResult.success(articleService.getArticleDraftInfo(req));
    }

    @ApiOperation("获取文章草稿列表")
    @GetMapping("/draft/list")
    public ApiResult<PageResp<ArticleInfoResp>> getArticleDraftList(@Valid ArticleListReq articleListReq){

        return ApiResult.success(articleService.getArticleDraftList(SecurityUtil.getLoginUser(), articleListReq));
    }


    @ApiOperation("管理员获取文章列表")
    @GetMapping("/admin/list")
    public ApiResult<PageResp<ArticleInfoResp>> adminArticleList(@Valid ArticleListReq req){

        return ApiResult.success(articleService.adminArticleList(req));
    }

    @ApiOperation("版主获取文章列表")
    @GetMapping("/plater/list")
    public ApiResult<PageResp<ArticleInfoResp>> platerArticleList(@Valid ArticleListReq req){

        return ApiResult.success(articleService.platerArticleList(SecurityUtil.getLoginUser(), req));
    }

    @ApiOperation("管理员修改文章状态")
    @PutMapping("/admin/update/status/{articleId}/{status}")
    public ApiResult<Void> adminUpdateStatus(@PathVariable("articleId") Long articleId, @PathVariable("status") Integer status){
        articleService.adminUpdateStatus(articleId, status);

        return ApiResult.success();
    }


}
